package com.miao.teacher.controls;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.espeaker.sdk.model.ReviseJson;
import com.google.gson.Gson;
import com.miao.teacher.models.ReviseInfo;

public class LocalRevise {
	/*
	 * 增加记录
	 */
	public static void doAddRevise(SQLiteDatabase sld,ReviseInfo info)
	{
		ContentValues cv = new ContentValues(); 
		cv.put("json", info.json);
		cv.put("is_end", 0);
		cv.put("xiaofei_id", info.xiaofei_id);
		sld.insert("reviser_his", "", cv);
	}
	/*
	 * 更新记录
	 */
	public static void doUpdateRevise(SQLiteDatabase sld,ReviseInfo info)
	{
		ContentValues cv = new ContentValues(); 
		cv.put("json", info.json);
		cv.put("xiaofei_id", info.xiaofei_id);
		sld.update("reviser_his", cv, "xiaofei_id=?", new String[]{info.xiaofei_id+""});
	}
	/*
	 * 更新记录
	 */
	public static void doUpdateReviseStatus(SQLiteDatabase sld,int xiaofei_id,int status)
	{
		ContentValues cv = new ContentValues(); 
		cv.put("is_end", status);
		cv.put("xiaofei_id", xiaofei_id);
		sld.update("reviser_his", cv, "xiaofei_id=?", new String[]{xiaofei_id+""});
	}
	/*
	 * 根据xiaofei_id 获取 如果不存在 就先建一条记录
	 */
	public static ReviseInfo getByXiaoFeiId(SQLiteDatabase sld,int xiaofei_id)
	{
		ReviseInfo info=new ReviseInfo();
		String mSQL="select * from reviser_his where xiaofei_id="+xiaofei_id;
		Cursor c = sld.rawQuery(mSQL, null);
		if(c.moveToNext())
		{
			info.id=c.getInt(c.getColumnIndex("id"));
			info.xiaofei_id=c.getInt(c.getColumnIndex("xiaofei_id"));
			info.json=c.getString(c.getColumnIndex("json"));
		}
		return info;
	}
	/*
	 * 获取历史
	 */
	public static List<ReviseJson> getHistory(SQLiteDatabase sld)
	{
		List<ReviseJson> mList=new ArrayList<ReviseJson>();
		
		String mSQL="select * from reviser_his where is_end=1";
		Cursor c = sld.rawQuery(mSQL, null);
		while(c.moveToNext())
		{
			String json=c.getString(c.getColumnIndex("json"));
			Gson gson = new Gson();
			mList.add(gson.fromJson(json, ReviseJson.class));
		}
		return mList;
	}
}
